package com.huluobo.a2110mvpimdemo.demo;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.huluobo.a2110mvpimdemo.R;
import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.uuzuche.lib_zxing.activity.ZXingLibrary;

public class QRCodeActivity extends AppCompatActivity {
    private ImageView imageView;
    private Button scan;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qrcode);
        imageView = findViewById(R.id.imageView);
        scan = findViewById(R.id.scan);

        requestPermissions(new String[]{"android.permission.CAMERA"}, 102);
        //二维码sdk初始化
        ZXingLibrary.initDisplayOpinion(this);

        String str = "dd1";
        //拿到当前资源包下的图片
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.weixin);

        Bitmap image = CodeUtils.createImage(str, 400, 400, bitmap);
        imageView.setImageBitmap(image);

        scan.setOnClickListener(v -> {
            Intent intent = new Intent(this, CaptureActivity.class);
            startActivityForResult(intent, 101);
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 101 && resultCode == RESULT_OK) {
            String result = data.getExtras().get(CodeUtils.RESULT_STRING).toString();
            Log.i("TagA", "二维码扫描结果:" + result);
        }
    }
}